package me.magicall.test.db.spring.dao2;

import me.magicall.db.Condition;
import me.magicall.db.ConditionOperator;
import me.magicall.db.OneFieldComparator;
import me.magicall.db.dao2.Dao2;
import me.magicall.db.dao2.Query;
import me.magicall.db.springjdbc.SpringJdbcTableMetaAccessor;
import me.magicall.db.springjdbc.dao2.DaoImpl;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;

public class DaoImplTest2 {

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
//		a();
//		b();
//		c();
		d();
//		e();
	}

	static void e() {//获取一批对象,成功
		final Dao2<T1Copy> dao = getDao();
		final Query<T1Copy> query = new Query<T1Copy>();
		query.addConditions(new Condition("name", ConditionOperator.STARTS_WITH, "a"), new Condition("id", ConditionOperator.IN, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
		query.setFieldComparator(OneFieldComparator.ID_DESC);
		System.out.println("@@@@@@DaoImplTest.e():" + dao.list(query));
	}

	static void d() {//获取一个对象,成功
		final Dao2<T1Copy> dao = getDao();
		System.out.println("@@@@@@DaoImplTest.d():" + dao.getById(1));
	}

	static void c() {//使用id删除一批对象,成功
		final Dao2<T1Copy> dao = getDao();
		System.out.println("@@@@@@DaoImplTest.c():" + dao.delById(15, 18));
	}

	static void b() {//删除一批对象,成功
		final Dao2<T1Copy> dao = getDao();
		final Query<T1Copy> query = new Query<T1Copy>();
		query.addConditions(new Condition("name", "test"), new Condition("id", ConditionOperator.IN, 16, 17));
		System.out.println("@@@@@@DaoImplTest.b():" + dao.del(query));
	}

	static void a() {//删除一个对象,成功
		final Dao2<T1Copy> dao = getDao();
		final Query<T1Copy> query = new Query<T1Copy>();
		query.addConditions(new Condition("name", "e"));
		System.out.println("@@@@@@DaoImplTest.a():" + dao.del(query));
	}

	static Dao2<T1Copy> getDao() {
		if (dao == null) {
			final NamedParameterJdbcOperations jdbc = U.getNamedJdbc();
			dao = new DaoImpl<T1Copy>(new SpringJdbcTableMetaAccessor(jdbc), jdbc, T1Copy.class);
		}
		return dao;
	}

	private static Dao2<T1Copy> dao;
}
